#ifndef _MUNIX_COMMON_H_
#define _MUNIX_COMMON_H_
/******************************************************************************
** Name: common.h                                                            **
** Description: Common declarations and code.                                **
**                                                                           **
** Open Source Initiative (OSI) Approved 2-clause BSD License                **
**                                                                           **
**  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
**  All rights reserved.                                                     **
**                                                                           **
**  Redistribution and use in source and binary forms, with or without       **
**  modification, are permitted provided that the following conditions are   **
**  met:                                                                     **
**                                                                           **
**  * Redistributions of source code must retain the above copyright notice, **
**    this list of conditions and the following disclaimer.                  **
**  * Redistributions in binary form must reproduce the above copyright      **
**    notice, this list of conditions and the following disclaimer in the    **
**    documentation and/or other materials provided with the distribution.   **
**                                                                           **
**  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
**                                                                           **
******************************************************************************/
// Modified $Date: 2011-10-22 02:09:07 +0000 (Sat, 22 Oct 2011) $ by $Author: gian.james $
#define _MUNIX_COMMON_ID    "$Id: common.hpp 146 2011-10-22 02:09:07Z gian.james $"

//#include <iostream>
#include <stdint.h>
#include <cwchar>
#include <string>

// Typedefs for shorthand
// I wonder how much time programmers around the world have lost from having
// to type "_t"...lolz
typedef uint8_t		uint8;
typedef int8_t      int8;
typedef uint16_t	uint16;
typedef int16_t		int16;
typedef uint32_t	uint32;
typedef int32_t		int32;
typedef unsigned char uchar;
typedef wchar_t     wchar;
typedef uint8_t		flags8;
typedef uint16_t	flags16;
typedef uint32_t	flags32;
typedef unsigned int	entity_t;

const std::string WHITESPACE = " \t\n\r";

//class Log;
//extern Log * log;

#define FNAME()			__PRETTY_FUNCTION__
#define CSTR(s)       	(#s)
#define DBGFUNC()       debug::log->out(LOG_DEBUG,"%s",__PRETTY_FUNCTION__)
#define WSTREQ(s1, s2)   ((std::wcscmp (s1, s2) == 0))
#define WNULL			L'\0'
#define MOTD			"../data/txt/MOTD"
#define WZERO(b,sz)		(wmemset(b,0,sz))
#define ZERO(b,sz)		(memset(b,0,sz))

// This just makes my life easier
//#define BIT16(b) ((unsigned long)0x00000001 << (b))
#define BIT32(b) ((uint32)0x00000001 << (b))
#define BIT16(b) ((uint16)0x0001 << (b))
#define	BIT8(b) (0x01 << (b))

// from AVR035: Efficient C Coding for AVR
#define BSET(ADDRESS,BIT) (ADDRESS |= (unsigned char)(1<<BIT))
#define BCLR(ADDRESS,BIT) (ADDRESS &= (unsigned char)~(1<<BIT))
#define BTOG(ADDRESS,BIT) (ADDRESS ^= (unsigned char)(1<<BIT))
#define BCHK(ADDRESS,BIT) (ADDRESS &  (unsigned char)(1<<BIT))

#define BMSET(x,y) (x |= (y))
#define BMCLR(x,y) (x &= (~y))
#define BMTOG(x,y) (x ^= (y))
#define BMCHK(x,y) (x & (y))

#ifndef Max
#define Max(x,y) ((x)>=(y)?(x):(y))
#endif

#ifndef Min
#define Min(x,y) ((x)<=(y)?(x):(y))
#endif

#ifndef Abs
#define Abs(x) ((x) < 0 ? -(x) : (x))
#endif

#ifndef PI
#define PI 3.1415927
#endif

#define METERCONV(METERS)	(METERS * 3.281)
#define FEETCONV(FEET)		(FEET / 3.281)
#define FAHRCONV(FAHRENHEIT)    ((FAHRENHEIT-32) * (5/9))
#define CELSIUSCONV(CELSIUS)    ((CELSIUS*1.8)+32)

#if defined(USE_MEM_MGMT)
// These should be moved into the memory management section when it gets written
void * operator new(size_t size);		// new replacement
void * operator new(size_t, void*);		// placement new
void * operator new[] (size_t size);
void operator delete(void * ptr);
void operator delete(void * p, void * const b);	// placement delete..hmmm
void operator delete[] (void * ptr);
#endif

#ifndef __ATTR_CONST__
#define __ATTR_CONST__ __attribute__((__const__))
#endif

#ifndef __ATTR_PURE__
#define __ATTR_PURE__ __attribute__((__pure__))
#endif

//! Takes a hex byte and returns a 2 character string representation
char    Hex2Char(char *);
//! Takes a 2 char string and converts it to an integer hex value
char *  Char2Hex(char);
int     Meters2Feet(int m);
int     Feet2Meters(int f);

std::string Uppercase(const std::string &);
std::string Lowercase(const std::string &);
std::string TrimWhiteSpace(const std::string &);
template< class type >
inline std::string toString(const type & t)
{
	std::stringstream str;
	str << t;
	return str.str();
}
template< class type >
inline type fromString(const std::string & s)
{
	std::stringstream str;
	str << s;
	type t;
	str >> t;
	return t;
}

struct Dice
{
    int   rolls:3;
    int   num:5;

    static int rnd;
    static int incr() { if (rnd >= 32) rnd = 0; return rnd++; }
    unsigned int   Roll();
};

enum class Locale {
    Alpine, Coniferous, Deciduous, Coastal, Glacier, Scrub, Jungle,
    Mountain, Marine, Grassland, Riparian, Underground, Volcanic,
    Detritus, Desert, Urban, Bog
};
enum class light_type_t { Dark, Shady, Average, Bright, Blinding };

#define WEIGHT_UNLIFTABLE   0





#endif	/* _MUNIX_COMMON_H_ */
